package com.dong.pointviewpager.adapter

import android.content.Context
import com.dong.pointviewpager.PVPManager
import com.dong.pointviewpager.interfaces.IPagerBean
import com.dong.pointviewpager.interfaces.listener.OnLoopPagerClickListener
import androidx.viewpager.widget.PagerAdapter
import android.view.ViewGroup
import com.dong.pointviewpager.widget.RoundImageView
import android.view.View
import com.dong.pointviewpager.config.LoopViewPagerConfig
import com.dong.pointviewpager.config.LoopViewPagerConfig.Companion.NUM_POSITION_CASE
import com.dong.pointviewpager.widget.LoopViewPager

/**
 * Created by Dong on 2018/3/13.
 */
class LoopPagerAdapter<T: IPagerBean>(private val context: Context, private val beans: List<T>, private val config: LoopViewPagerConfig) : PagerAdapter() {

    var onLoopPagerClickListener: OnLoopPagerClickListener<T>? =null

    override fun getCount(): Int {
        beans?.run {
            return if(config.isLoop){
                when(size){
                    0, 1 -> size
                    else -> size + NUM_POSITION_CASE * 2
                }
            }else{
                size
            }
        }
        return 0
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val imageView = RoundImageView(context)
        imageView.scaleType = config.imageScale
        imageView.setRadius(config.radius)
        if (beans != null && beans.isNotEmpty()) loadImage(context, imageView, beans[getDataPosition(position)])
        imageView.setOnClickListener {
            onLoopPagerClickListener?.onLoopPagerClick(getDataPosition(position), beans[getDataPosition(position)])
        }
        container.addView(imageView)
        return imageView
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }

    /**
     * 获取数据位置
     */
    fun getDataPosition(position : Int): Int{
        if(!config.isLoop || beans == null) return position

        var dataPosition = position - NUM_POSITION_CASE
        dataPosition = (dataPosition + beans!!.size)%beans.size
        return dataPosition
    }

    /**
     * 获取映射位置
     */
    fun getViewPosition(position : Int): Int{
        if(!config.isLoop || beans == null) return position

        var mapPosition = position + NUM_POSITION_CASE
        mapPosition %= count
        return mapPosition
    }

    /**
     * 定位到实际位置
     */
    fun setRealCurrentItem(loopViewPager: LoopViewPager<T>, currentItem: Int){
        if(config.isLoop && count > 1){
            if (currentItem == NUM_POSITION_CASE - 1) {
                loopViewPager.setCurrentItem(count - 1 - NUM_POSITION_CASE, false)
                return
            }
            if (currentItem == count - NUM_POSITION_CASE) {
                loopViewPager.setCurrentItem(NUM_POSITION_CASE, false)
                return
            }
        }
    }

    /**
     * 获取起始位置
     */
    fun getStartPosition(): Int{
        return if(config.isLoop && beans.size > 1) NUM_POSITION_CASE else 0
    }

    private fun loadImage(context: Context, imageView: RoundImageView, bean: IPagerBean) {
        PVPManager.imageLoader?.loadImage(context, imageView, bean.pagerUrl?:"", bean.placeholderRes, bean.errorRes)
    }
}